package com.leetcode.sorts.tree;

/**
 * @Author: WuPeng
 * @Date: 2021/6/10 4:37 下午
 *
 * 236. 二叉树的最近公共祖先
 *
 */
public class BinarySearchTree_236 {

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (p == root || q == root || root == null) return root;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);

        if (left != null && right != null) return root;
        if (left == null) return right;
        return left;
    }

}
